const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
const operateMethod = require('../service/operatef.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bibOption: [],
        biddingSituation: '',
        communicateOption: [],
        preBidCommunication: '',
        treetopList: [],
        reasonList: [],
        dataList: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('options', options)
        let that = this
        that.setData({
            taskId: options.taskId,
            operationType: options.type,
            hospitalId: options.hospitalId,
            taskType: options.taskType,
            operationlist: options.operationlist ? JSON.parse(options.operationlist) : [],
            operationTypeCode: options.operationTypeCode,
            activeKey: options.activeKey
        })
        that.getDetailById()
        that.getProjectList('friendly_business')
        that.getProjectList('loseBId_reason')
        that.getCategoryListByParentCode('B26')
        that.getCategoryListByParentCode('B26A02')
        if (options.type == 'in_invite_bid') {
            wx.setNavigationBarTitle({
                title: '院内招标说标'
            })
        } else if (options.type == 'assist_invite_bid') {
            wx.setNavigationBarTitle({
                title: '协助代理商招标说标'
            })
        }
    },
    //获取任务详情
    getDetailById() {
        let that = this
        lbbTaskService.getDetailById({
            taskId: that.data.taskId
        }, function (res) {
            that.setData({
                taskProjectText: res.data.taskProjectText,
                taskClassificationName: res.data.taskClassificationName,
                hospitalName: res.data.hospitalName,
                taskDesc: res.data.taskDesc,
                taskImages: res.data.taskImages ? res.data.taskImages.split(',') : [],
                isUrgent: res.data.isUrgent
            })
        })
    },
    //获取数据字典值
    getProjectList(code) {
        let that = this
        lbbTaskService.getDictByCode({
            code: code
        }, function (res) {
            if (code == 'friendly_business') {
                that.setData({
                    treetopList: res.data
                })
            } else if (code == 'loseBId_reason') {
                that.setData({
                    reasonList: res.data
                })
            }
            that.getInInviteBid()
        })
    },
    //获取分类字典值
    getCategoryListByParentCode(code) {
        let that = this
        lbbTaskService.getCategoryListByParentCode({
            code: code
        }, function (res) {
            if (code == 'B26') {
                that.setData({
                    bibOption: res.data
                })
            } else if (code == 'B26A02') {
                that.setData({
                    communicateOption: res.data
                })
            }
        })
    },
    //回显
    getInInviteBid() {
        let that = this
        lbbTaskService.getInInviteBid({
            taskId: that.data.taskId,
            operationType: that.data.operationType
        }, function (res) {
            console.log(res)
            if (res.data) {
                res.data.biddingSituationText = that.data.bibOption.find(item => item.value == res.data.biddingSituation).label
                if (res.data.biddingSituation == '1643930025165185025') {
                    res.data.preBidCommunicationText = that.data.communicateOption.find(item => item.value == res.data.preBidCommunication).label
                    res.data.bidderWinnerText = that.data.treetopList.find(item => item.value == res.data.bidderWinner).text
                    res.data.notBidReasonText = that.data.reasonList.find(item => item.value == res.data.notBidReason).text
                }

                that.setData({
                    dataList: res.data
                })
                console.log(85858, res.data)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    //选择投标情况
    onBindBid(e) {
        this.setData({
            bidIfShow: true
        })
    },
    //选择投标情况回调
    okBid(e) {
        this.data.dataList.biddingSituationText = e.detail.name
        this.data.dataList.biddingSituation = e.detail.id
        this.setData({
            bidIfShow: false,
            dataList: this.data.dataList
        })
    },
    //标前沟通情况
    onBindpreBid(e) {
        this.setData({
            preBidIfShow: true
        })
    },
    //标前沟通情况回调
    okpreBid(e) {
        this.data.dataList.preBidCommunicationText = e.detail.name
        this.data.dataList.preBidCommunication = e.detail.id
        this.setData({
            preBidIfShow: false,
            dataList: this.data.dataList
        })
    },
    //打开中标方列表
    onBindbidder() {
        this.setData({
            bidderIfShow: true
        })
    },
    //选择中标方回调
    okTreetop(e) {
        this.data.dataList.bidderWinnerText = e.detail.name
        this.data.dataList.bidderWinner = e.detail.id
        this.setData({
            bidderIfShow: false,
            dataList: this.data.dataList
        })
    },
    //打开未中标原因弹窗
    onBindReason() {
        this.setData({
            reasonIfShow: true
        })
    },
    //选择原因回调
    okReason(e) {
        this.data.dataList.notBidReasonText = e.detail.name
        this.data.dataList.notBidReason = e.detail.id
        this.setData({
            reasonIfShow: false,
            dataList: this.data.dataList
        })
    },
    //备注信息
    changeRemark(e) {
        this.data.dataList.remark = e.detail.value ? e.detail.value : ''
        this.setData({
            dataList: this.data.dataList
        })
    },
    //提交
    btnSubmit() {
        let that = this
        let obj = {
            ...that.data.dataList,
            taskId: that.data.taskId,
            hospitalId: that.data.hospitalId,
            operationType: that.data.operationType
        }
        console.log(obj)
        if (obj.biddingSituation == '1643930025165185025') {
            if (obj.bidderWinner && obj.notBidReason && obj.biddingSituation && obj.preBidCommunication) {
                lbbTaskService.inInviteBid({
                    inInviteBidInfo: JSON.stringify(obj)
                }, function (res) {
                    wx.navigateBack({
                        delta: 1
                    })
                })
            } else {
                that.openPrompt()
            }
        } else if (!obj.biddingSituation) {
            that.openPrompt()
        } else {
            obj.preBidCommunication = ''
            obj.bidderWinner = ''
            obj.notBidReason = ''
            lbbTaskService.inInviteBid({
                inInviteBidInfo: JSON.stringify(obj)
            }, function (res) {
                if (that.data.activeKey == 3) {
                    wx.navigateBack({
                        delta: 1
                    })
                } else {
                    operateMethod(that.data.operationlist, that.data.taskId, that.data.hospitalId, that.data.taskType, that.data.operationTypeCode)
                }
            })
        }

    },
    //校验提示
    openPrompt() {
        wx.showToast({
            title: '请检查必填项',
            icon: 'error',
            duration: 2000
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})